package com.c201801020208.ybl.Adapters;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import androidx.recyclerview.widget.RecyclerView;

import com.c201801020208.ybl.Company.Activity.AddJobActivity;
import com.c201801020208.ybl.R;

import com.c201801020208.ybl.Company.Activity.JobInfoActivity;
import com.c201801020208.ybl.Company.Activity.Main2Activity;
import com.c201801020208.ybl.Tables.CompanyInfo;
import com.c201801020208.ybl.Tables.MyJobInfo;
import com.c201801020208.ybl.User.Activity.MainActivity;

import java.util.ArrayList;
import java.util.List;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;
import cn.bmob.v3.listener.SaveListener;

public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> {
    private Context mContext;
    String type;
    List<CompanyInfo> mList = new ArrayList<>();
    MainActivity mainActivity;
    String myusername;

    public RecyclerViewAdapter(Context mContext, String type) {
        this.mContext = mContext;
        this.type = type;
        BmobQuery<CompanyInfo> categoryBmobQuery = new BmobQuery<>();
        categoryBmobQuery.addWhereEqualTo("type", type);
        categoryBmobQuery.findObjects(new FindListener<CompanyInfo>() {
            @Override
            public void done(List<CompanyInfo> object, BmobException e) {
                if (e == null) {
//                    Toast.makeText(mContext, "查询成功：" + object.size(), Toast.LENGTH_LONG).show();
                    for (int i = 0; i < object.size(); i++) {
                        CompanyInfo companyInfo = new CompanyInfo();
                        companyInfo.setUsername(object.get(i).getUsername());
                        companyInfo.setAddress(object.get(i).getAddress());
                        companyInfo.setWorktime(object.get(i).getWorktime());
                        companyInfo.setWorkexperience(object.get(i).getWorkexperience());
                        companyInfo.setCompanyname(object.get(i).getCompanyname());
                        companyInfo.setMoney(object.get(i).getMoney());
                        companyInfo.setType(object.get(i).getType());
                        companyInfo.setJobname(object.get(i).getJobname());
                        companyInfo.setEducation(object.get(i).getEducation());
                        companyInfo.setDescribe(object.get(i).getDescribe());
                        mList.add(companyInfo);
                    }
                    notifyDataSetChanged();
                } else {
                    Log.e("BMOB", e.toString());
//                    Toast.makeText(mContext, e.getMessage(), Toast.LENGTH_LONG).show();
                }
            }
        });

    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.chatlayout, parent, false);
        return new ViewHolder(view);
    }

    @Override//holder用来给加载控件，控件赋值，需要在外面加载list数据
    public void onBindViewHolder(final ViewHolder holder, int position) {
        CompanyInfo companyInfo = mList.get(position);
        holder.address.setText(companyInfo.getAddress());
        holder.companyname.setText(companyInfo.getCompanyname());
        holder.jobname.setText(companyInfo.getJobname());
        holder.education.setText(companyInfo.getEducation());
        holder.workexperience.setText(companyInfo.getWorkexperience());
        holder.content.setText(companyInfo.getDescribe());
        holder.salary.setText(companyInfo.getMoney());
        holder.worktime.setText(companyInfo.getWorktime());
        holder.jobtype.setText(companyInfo.getType());
        holder.username.setText(companyInfo.getUsername());
        //rootView控件跳转
        holder.mView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MyJobInfo myJobInfo = new MyJobInfo();
                myusername = mainActivity.getUsername();
                myJobInfo.setMyname(myusername);
                myJobInfo.setUsername(holder.username.getText().toString());
                myJobInfo.setCompanyname(holder.companyname.getText().toString());
                myJobInfo.setType(holder.jobtype.getText().toString());
                myJobInfo.setJobname(holder.jobname.getText().toString());
                myJobInfo.setMoney(holder.salary.getText().toString());
                myJobInfo.setWorkexperience(holder.workexperience.getText().toString());
                myJobInfo.setEducation(holder.education.getText().toString());
                myJobInfo.setAddress(holder.address.getText().toString());
                myJobInfo.setWorktime(holder.worktime.getText().toString());
                myJobInfo.setDescribe(holder.content.getText().toString());
                myJobInfo.save(new SaveListener<String>() {
                    @Override
                    public void done(String objectId, BmobException e) {
                        if (e == null) {
//                            Toast.makeText(mContext, "添加数据成功，返回objectId为：" + objectId, Toast.LENGTH_LONG);

                        } else {
//                            Toast.makeText(mContext, "创建数据失败：" + e.getMessage(), Toast.LENGTH_LONG);
                        }
                    }
                });


                Intent intent = new Intent(v.getContext(), JobInfoActivity.class);
                intent.putExtra("companyInfo", companyInfo);
                v.getContext().startActivity(intent);
            }
        });

    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        public final View mView;
        TextView jobname;
        TextView companyname;
        TextView salary;
        TextView username;
        TextView address;
        TextView worktime;
        TextView jobtype;
        TextView education;
        TextView workexperience;
        TextView content;


        public ViewHolder(View view) {
            super(view);
            mView = view;
            jobname = view.findViewById(R.id.textView9);
            companyname = view.findViewById(R.id.textView13);
            salary = view.findViewById(R.id.textView14);
            username = view.findViewById(R.id.textView7);
            address = view.findViewById(R.id.textView8);
            worktime = view.findViewById(R.id.textView15);
            jobtype = view.findViewById(R.id.textView16);
            content = view.findViewById(R.id.textView17);
            workexperience = view.findViewById(R.id.textView18);
            education = view.findViewById(R.id.textView20);
        }
    }
}
